package static_variable;

/**
 * @Description: 饿汉式， 静态变量方式
 * @Author: WangBlue
 * @Create: 2024-09-23 14:20
 * @Version: 1.0
 **/

public class HungryStyleStaticVariable {
    // 私有化方法构造器
    private HungryStyleStaticVariable() { }  // 构造器私有化的目的是为了防止类的实例化

    // 在成员位置创建该类对象
    private static HungryStyleStaticVariable instance = new HungryStyleStaticVariable();

    // 提供公共方法获取实例对象
    public static HungryStyleStaticVariable getInstance() {
        return instance;
    }

}

class Test {
    public static void main(String[] args) {
        HungryStyleStaticVariable instance = HungryStyleStaticVariable.getInstance();
        HungryStyleStaticVariable instance1 = HungryStyleStaticVariable.getInstance();
        if (instance == instance1) {
            System.out.println(" instance 和 instance1 指向同一个对象");
        }
    }
}
